package com.delong.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.delong.dao.CategoryMapper;
import com.delong.dao.DaoHelper;
import com.delong.model.app.Category;

public class CategoryService {

	public List<Category> getListElement() {
		SqlSession session = null;
		List<Category> result = new ArrayList<Category>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CategoryMapper mapper = session.getMapper(CategoryMapper.class);
			result = mapper.selectALLCategory();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return result;

	}

	public List<Category> selectCategoryByTitle(String title, int page, int rows) {
		SqlSession session = null;
		List<Category> result = new ArrayList<Category>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CategoryMapper mapper = session.getMapper(CategoryMapper.class);
			result = mapper.selectCategoryByTitle(title, page, rows);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public Category selectCategoryById(int id) {
		SqlSession session = null;
		Category result = new Category();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CategoryMapper mapper = session.getMapper(CategoryMapper.class);
			result = mapper.selectCategoryById(id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public int selectCount(String title) {
		SqlSession session = null;
		int result = 0;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CategoryMapper mapper = session.getMapper(CategoryMapper.class);
			result = mapper.selectCount(title);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public void addElement(Category element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CategoryMapper mapper = session.getMapper(CategoryMapper.class);
			mapper.addElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public void changeElement(Category element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CategoryMapper mapper = session.getMapper(CategoryMapper.class);
			mapper.changeCategory(element);
//			System.out.println("change category");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public void changeStatus(int id, int status) {
		SqlSession session = null;

		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CategoryMapper mapper = session.getMapper(CategoryMapper.class);
			mapper.changeStatus(id, status);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}
}
